import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:pornhub_app/components/no_more/no_data.dart';
import 'package:pornhub_app/components/short_widget/video_base_cell.dart';
import 'package:pornhub_app/http/http_service.dart';
import 'package:pornhub_app/model/video_base_model.dart';

class TagVideoListPage extends StatefulWidget {
  const TagVideoListPage({super.key});

  @override
  State<TagVideoListPage> createState() => _TagVideoListPageState();
}

class _TagVideoListPageState extends State<TagVideoListPage> {
  final EasyRefreshController refreshController = EasyRefreshController();
  final PagingController<int, VideoBaseModel> pagingControllers =
      PagingController(firstPageKey: 1);
  int pageIndex = 1;
  List<String> tagId = [];
  String tagName = "";

  getTagVideoList({required bool isRefresh}) {
    pageIndex = isRefresh ? 1 : pageIndex++;
    httpInstance
        .get(
      url: 'video/getByClassify',
      queryMap: {
        'page': pageIndex,
        'pageSize': 20,
        'tagsId': tagId,
      },
      complete: VideoBaseModel.fromJson,
    )
        .then((response) {
      if (response != null && response.isNotEmpty) {
        if (isRefresh) {
          pagingControllers.refresh();
        }
        pagingControllers.appendPage(response, pageIndex);
      } else {
        pagingControllers.appendLastPage([]);
      }
    });
  }

  @override
  void initState() {
    tagId = Get.arguments['tagId'];
    tagName = Get.arguments['tagName'];
    super.initState();
    getTagVideoList(isRefresh: true);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(tagName)),
      body: _bodyView(),
    );
  }

  _bodyView() {
    return EasyRefresh(
      controller: refreshController,
      refreshOnStart: false,
      onRefresh: () => getTagVideoList(isRefresh: true),
      onLoad: () => getTagVideoList(isRefresh: false),
      child: PagedGridView<int, VideoBaseModel>(
        padding: EdgeInsets.only(top: 10, left: 14.w, right: 14.w),
        addAutomaticKeepAlives: true,
        pagingController: pagingControllers,
        builderDelegate: PagedChildBuilderDelegate<VideoBaseModel>(
          firstPageProgressIndicatorBuilder: (context) =>
              const SizedBox.shrink(),
          newPageProgressIndicatorBuilder: (context) => const SizedBox.shrink(),
          noMoreItemsIndicatorBuilder: (context) => const SizedBox.shrink(),
          noItemsFoundIndicatorBuilder: (context) => const NoData(),
          itemBuilder: (context, item, index) =>
              VideoBaseCell.small(video: item),
        ),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            mainAxisSpacing: 10.w,
            crossAxisSpacing: 8.w,
            crossAxisCount: 2,
            childAspectRatio: 182 / 134),
      ),
    );
  }
}
